package com.splic.product.service.impl;

import java.util.Date;
import java.util.List;
import java.util.function.Consumer;

import com.splic.common.utils.DateUtils;
import com.splic.product.domain.UserSetData;
import com.splic.product.dto.UserSearchKeywordStatisDto;
import com.splic.product.mapper.UserSearchKeywordRecordMapper;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.splic.product.mapper.HotsearchKeywordMapper;
import com.splic.product.domain.HotSearchKeyword;
import com.splic.product.service.HotsearchKeywordService;
import org.springframework.transaction.annotation.Transactional;

/**
 * 热搜关键词Service业务层处理
 * 
 * @author singe
 * @date 2022-01-04
 */
@Service
public class HotsearchKeywordServiceImpl implements HotsearchKeywordService
{
    @Autowired
    private HotsearchKeywordMapper hotsearchKeywordMapper;
    @Autowired
    private UserSearchKeywordRecordMapper searchKeywordRecordMapper;

    /**
     * 查询热搜关键词
     * 
     * @param id 热搜关键词主键
     * @return 热搜关键词
     */
    @Override
    public HotSearchKeyword selectHotsearchKeywordById(Long id)
    {
        return hotsearchKeywordMapper.selectHotsearchKeywordById(id);
    }

    /**
     * 查询热搜关键词列表
     * 
     * @param hotSearchKeyword 热搜关键词
     * @return 热搜关键词
     */
    @Override
    public List<HotSearchKeyword> selectPageList(HotSearchKeyword hotSearchKeyword)
    {
        return hotsearchKeywordMapper.selectHotsearchKeywordList(hotSearchKeyword);
    }

    /**
     * 查询热搜关键词列表
     *
     * @param hotSearchKeyword 热搜关键词
     * @return 热搜关键词
     */
    @Override
    public List<HotSearchKeyword> selectHotsearchKeywordList(HotSearchKeyword hotSearchKeyword)
    {
        return hotsearchKeywordMapper.selectHotsearchKeywordList(hotSearchKeyword);
    }

    /**
     * 新增热搜关键词
     * 
     * @param hotSearchKeyword 热搜关键词
     * @return 结果
     */
    @Override
    public int insert(HotSearchKeyword hotSearchKeyword)
    {
        hotSearchKeyword.setCreateTime(DateUtils.getNowDate());
        return hotsearchKeywordMapper.insert(hotSearchKeyword);
    }

    /**
     * 修改热搜关键词
     * 
     * @param hotSearchKeyword 热搜关键词
     * @return 结果
     */
    @Override
    public int update(HotSearchKeyword hotSearchKeyword)
    {
        hotSearchKeyword.setUpdateTime(DateUtils.getNowDate());
        return hotsearchKeywordMapper.update(hotSearchKeyword);
    }


    /**
     * 删除热搜关键词信息
     * 
     * @param id 热搜关键词主键
     * @return 结果
     */
    @Override
    public int deleteById(Long id)
    {
        return hotsearchKeywordMapper.deleteById(id);
    }
    /**
     * 修改热搜关键词
     *
     * @param hotSearchKeyword 热搜关键词信息
     * @return 结果
     */
    @Override
    @Transactional
    public int saveOrUpdate(HotSearchKeyword hotSearchKeyword)
    {
        if(hotSearchKeyword.getId() != null){
            hotSearchKeyword.setUpdateTime(new Date());
            return hotsearchKeywordMapper.update(hotSearchKeyword);
        }else{
            hotSearchKeyword.setCreateTime(new Date());
            return hotsearchKeywordMapper.insert(hotSearchKeyword);
        }
    }


    /**
     * 统计热搜词
     */
    @Override
    public void statisticsSearch() {
        Date yestedayDate = org.apache.commons.lang3.time.DateUtils.addDays(new Date(),-1);
        String yesdayStr = DateUtils.dateTime(yestedayDate);

        List<UserSearchKeywordStatisDto> userSearchList = searchKeywordRecordMapper.selectUserSearchKeywordStistics(yesdayStr);
        HotSearchKeyword hotSearchKeyword = null;
        if(CollectionUtils.isNotEmpty(userSearchList)){
            int index = 1;
            Date currentDate = new Date();
            for(UserSearchKeywordStatisDto dto :userSearchList){
                hotSearchKeyword = new HotSearchKeyword();
                hotSearchKeyword.setKeyword(dto.getKeyword());
                hotSearchKeyword.setHot(Long.valueOf(dto.getNum()));
                hotSearchKeyword.setSort(index++);
                hotSearchKeyword.setCreateTime(currentDate);
                hotsearchKeywordMapper.insert(hotSearchKeyword);
            }
        }
    }
}
